#include "main.h"
#include "time5.h"

/**
 * @brief   TMR configuration
 *
 * @param   None
 *
 * @retval  None
 */
void TMR5_Config(void)
{
    TMR_BaseConfig_T tmrBaseConfig;
    TMR_OCConfig_T tmrOCConfig;
    GPIO_Config_T gpioConfig;
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR5);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);

    gpioConfig.speed = GPIO_SPEED_50MHz;
    gpioConfig.mode = GPIO_MODE_AF_PP;
    gpioConfig.pin = GPIO_PIN_1;
    GPIO_Config(GPIOA, &gpioConfig);

    /* TMR5 clock source frequency = 120MHz */
    tmrBaseConfig.countMode = TMR_COUNTER_MODE_UP;
    tmrBaseConfig.clockDivision = TMR_CLOCK_DIV_1;
    tmrBaseConfig.period = 999;
    tmrBaseConfig.division = 59;
    tmrBaseConfig.repetitionCounter = 0;
    TMR_ConfigTimeBase(TMR5, &tmrBaseConfig);
    /* 50% PWM */
    tmrOCConfig.mode = TMR_OC_MODE_PWM1;
    tmrOCConfig.outputState = TMR_OC_STATE_ENABLE;
    tmrOCConfig.outputNState = TMR_OC_NSTATE_DISABLE;
    tmrOCConfig.polarity = TMR_OC_POLARITY_HIGH;
    tmrOCConfig.nPolarity = TMR_OC_NPOLARITY_HIGH;
    tmrOCConfig.idleState = TMR_OC_IDLE_STATE_RESET;
    tmrOCConfig.nIdleState = TMR_OC_NIDLE_STATE_RESET;
    tmrOCConfig.pulse = 500;
    TMR_ConfigOC2(TMR5, &tmrOCConfig);

    TMR_EnablePWMOutputs(TMR5);
    TMR_Enable(TMR5);
}



void UpdataFre(int data)
{
		int k ;

		k= 2000000 /data ;

    TMR_BaseConfig_T tmrBaseConfig;
    TMR_OCConfig_T tmrOCConfig;

    tmrBaseConfig.period = k;
    TMR_ConfigTimeBase(TMR5, &tmrBaseConfig);

    tmrOCConfig.pulse = k / 2;
    TMR_ConfigOC2(TMR5, &tmrOCConfig);
}




void UpdataDuty(int data)
{
    TMR_BaseConfig_T tmrBaseConfig;
    TMR_OCConfig_T tmrOCConfig;

    tmrBaseConfig.period = 999;
    TMR_ConfigTimeBase(TMR5, &tmrBaseConfig);

    tmrOCConfig.pulse = data / 100 *1000;
    TMR_ConfigOC2(TMR5, &tmrOCConfig);
}









